package com.wxq.day0421.controller;

import com.wxq.day0421.pojo.FileDomain;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;

/**
 * @Author wxq
 * @Date 2021/4/21
 **/
@Controller
public class FileUploadController {
    private static final Log logger = LogFactory.getLog(FileUploadController.class);

    /**
     * 单文件上传
     */
    @RequestMapping("/onefile")
    public String oneFileUpload(@ModelAttribute FileDomain fileDomain) {
        //文件上传到服务器的位置
        String realPath = "D:\\upload";
        String fileName = fileDomain.getMyfile().getOriginalFilename();
        File targetFile = new File(realPath, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        //上传
        try {
            fileDomain.getMyfile().transferTo(targetFile);
            logger.info("成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "showOne";
    }
}

